<?php

namespace App\Http\Controllers\Company;

use App\Http\Controllers\Controller;
use App\Models\StudentModel;
use Illuminate\Http\Request;

class StudentController extends Controller
{
    public function page(Request $request){
        $query = StudentModel::where('company_id',$request->company_id);
        if ($request->has('name') && $request->name){
            $query->where('name', 'like', '%'.$request->name.'%');
        }
        $pageInfo = $query->orderBy('id', 'desc')
        ->paginate($request->pageSize, ['*'], 'page', $request->page);
        return self::ok($pageInfo);
    }

    public function add(Request $request){
        $rules = ['name' => 'required'];
        $messages = ['name.required' => '姓名不能为空'];
        $student = new StudentModel();
        $student->fill($request->all());
        $student->save();
        return self::ok($student);
    }
    public function edit(Request $request){
        $student = StudentModel::where('company_id', $request->company_id)
            ->where('id', $request->id)
            ->first();
        $student->fill($request->all());
        $student->save();
        return self::ok($student);
    }
    public function delete(Request $request){
        StudentModel::where('company_id', $request->company_id)
            ->where('id', $request->id)
            ->delete();
        // 删除学生要退出所在班级

        return self::ok();
    }
    public function info(Request $request){
        
    }
}
